package util;



import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class DBWrapper {

	public static final String DB_DRIVER_MYSQL = "com.mysql.jdbc.Driver";

	private Connection connection;

	private String hostname;
	private String hostport;
	private String username;
	private String password;
	private String dbname;

	public DBWrapper(String hostname, String hostport, String dbname) {
		this.hostname = hostname;
		this.hostport = hostport;
		this.dbname   = dbname;
		this.connection = null;
	}

	public DBWrapper(String hostname, String hostport, String username, String password, String dbname) {
		this.hostname = hostname;
		this.hostport = hostport;
		this.password = password;
		this.username = username;
		this.dbname   = dbname;
		this.connection = null;
	}

	public DBWrapper(String hostname, String hostport, String username, String password) {
		this.hostname = hostname;
		this.hostport = hostport;
		this.password = password;
		this.username = username;
        this.connection = null;
	}

	public DBWrapper() {
		this.hostname = "localhost";
		this.password = "ase45f66";
		this.username = "root";
		this.hostport = "3306";
		this.dbname   = "test1";
		this.connection = null;
	}

	public void connect() {
		String connUrl = "jdbc:mysql://"+this.hostname+":"+this.hostport+"/"+this.dbname;
		try {
			Class.forName (DB_DRIVER_MYSQL).newInstance();
		} catch (Exception e) {
			log("Check classpath. Cannot load db driver: " + DB_DRIVER_MYSQL);
		}
		try {
			this.connection = DriverManager.getConnection (connUrl,this.username, this.password);
		} catch (SQLException ex) {
			log("Driver loaded, but cannot connect to db: " + connUrl);
		}
	}

	public void connect(String username, String password) {
		this.username = username;
		this.hostport = password;

		String connUrl = "jdbc:mysql://"+this.hostname+":"+this.hostport+"/"+this.dbname;
		try {
			Class.forName (DB_DRIVER_MYSQL).newInstance();
		} catch (Exception e) {
			log("Check classpath. Cannot load db driver: " + DB_DRIVER_MYSQL);
		}
		try {
			this.connection = DriverManager.getConnection (connUrl,this.username, this.password);
		} catch (SQLException ex) {
			log("Driver loaded, but cannot connect to db: " + connUrl);
		}
	}

	public Connection getConnection() {
            if (this.connection == null)
                this.connect();
            return connection;
	}

	public String getHostname() {
		return hostname;
	}

	public void setHostname(String hostname) {
		this.hostname = hostname;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getHostport() {
		return hostport;
	}

	public void setHostport(String hostport) {
		this.hostport = hostport;
	}

	public String getDbname() {
		return dbname;
	}

	public void setDbname(String dbname) {
		this.dbname = dbname;
	}

    @Override
    protected void finalize() throws Throwable {
        super.finalize();
        this.connection.close();
    }



	private static void log(Object aObject){
	    System.out.println(aObject);
	}
}
